{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "50e13f55",
   "metadata": {},
   "source": [
    "# 高斯玻色采样(Gaussian Boson Sampling)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ff195be",
   "metadata": {},
   "source": [
    "## 数学背景"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a340b83b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-28T01:46:58.387781Z",
     "start_time": "2024-05-28T01:46:58.382945Z"
    }
   },
   "source": [
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/gbs.png\" width=\"40%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "高斯玻色采样(GBS)可以作为玻色采样的一种变体，不同之处在于输入的量子态是高斯压缩态而不是离散的Fock态。\n",
    "压缩态是高斯态的一种，高斯态指的是这个量子态对应的Wigner函数是高斯分布，比如相干态。\n",
    "单模压缩态的Wigner函数对应的高斯分布在 $X$，$P$ 两个正交分量上会压缩或者拉伸，单模压缩态可以将压缩门作用到真空态上得到，也可以用下面的Fock态基矢展开[2]，需要注意的是这里的Fock态光子数从0到无穷大取偶数，因此输出的量子态的空间是无限大且只包含偶数光子数的Fock态空间。\n",
    "\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/s.png\" width=\"40%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "GBS采样概率的理论计算和玻色采样类似，不同之处在于对粒子数分辨探测器和阈值探测器两种情况，分别需要用hafnian函数和torotonian函数来计算。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6a1b43e",
   "metadata": {},
   "source": [
    "1. 粒子数分辨探测器\n",
    "\n",
    "在探测端口使用粒子数分辨探测器时对应数学上需要计算hafnian函数，\n",
    "对于 $2m\\times 2m$ 对称矩阵 $A$ 的hafnian定义如下[3]，\n",
    "\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/haf.png\" width=\"30%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "这里PMP表示所有完美匹配排列的集合，当 $n=4$ 时，$PMP(4) = {(0,1)(2,3),(0,2)(1,3),(0,3)(1,2)}$，对应的矩阵 $B$ 对应的hafnian如下\n",
    "\n",
    "$$\n",
    "haf(B) = B_{0,1}B_{2,3}+B_{0,2}B_{1,3} + B_{0,3}B_{1,2}\n",
    "$$\n",
    "\n",
    "在图论中，hafnian计算了图 $G$ 对应的邻接矩阵A描述的图的完美匹配数(这里图 $G$ 是无权重，无环的无向图)，比如邻接矩阵\n",
    "$A =\\begin{pmatrix}\n",
    "0&1&1&1\\\\\n",
    "1&0&1&1\\\\\n",
    "1&1&0&1\\\\\n",
    "1&1&1&0\n",
    "\\end{pmatrix}$，haf(A)=3，对应的完美匹配图如下。\n",
    "\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/f9.png\" width=\"30%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "当计算的图是二分图时，得到的hafnian计算结果就是permanent。\n",
    "\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/per.png\" width=\"40%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "因此任何计算hafnian的算法也可以用来计算permanent，同样的计算hafnian也是 $\\#P$ 难问题。\n",
    "\n",
    "对于粒子数探测器，输出的Fock态 $S = (s_1, s_2,..,s_m)$ 时，对应的 $s_i=0,1,2...$，\n",
    "输出态的概率理论计算如下\n",
    "\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/f5.png\" width=\"30%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "这里 $Q,A,X$ 的定义如下，\n",
    "\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/f6.png\" width=\"20%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "$Q,A$ 由输出量子态的协方差矩阵 $\\Sigma$ 决定 ( $\\Sigma$ 描述的是 $a,a^+$ 的协方差矩阵)，子矩阵 $A_s$\n",
    "由输出的Fock态决定，具体来说取矩阵 $A$ 的 $i, i+m$ 行和列并且重复 $s_i$ 次来构造 $A_s$ 。\n",
    "如果 $s_i=0$，那么就不取对应的行和列，如果所有的 $s_i=1$, 那么对应的子矩阵 $A_s = A$。\n",
    "\n",
    "考虑高斯态是纯态的时候， 矩阵$A$可以写成直和的形式，$A = B \\oplus B^*$, $B$ 是 $m\\times m$ 的对称矩阵。这种情况下输出Fock态的概率如下\n",
    "\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/f4.png\" width=\"30%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "这里的子矩阵 $B_s$ 通过取 $i$ 行和 $i$ 列并且重复 $s_i$ 次来构造，同时这里hafnian函数计算的矩阵维度减半，可以实现概率计算的加速。\n",
    "\n",
    "当所有模式输出的光子数 $s_i = 0,1$ 时，对应的 $A_s$ 是A的子矩阵，也对应到邻接矩阵A对应的图 $G$ 的子图，利用这个性质可以解决很多子图相关的问题，比如稠密子图，最大团问题等。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c49e11f",
   "metadata": {},
   "source": [
    "2. 阈值探测器\n",
    "\n",
    "使用阈值探测器时对应的输出Fock态概率 $S = (s_1, s_2,..,s_m)，s_i\\in \\{0,1\\}$，此时理论概率的计算需要用到Torontonian函数[4]\n",
    "\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/f7.png\" width=\"20%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "<div style=\"margin-right: 15px; border-radius: 10px; background-color: rgb(255， 255， 255); text-align: center;\">\n",
    "    <img src=\"./fig/f8.png\" width=\"40%\"/>\n",
    "    <p style=\"padding: 10px; font-size: small; text-align: center; line-height: 0%;\">\n",
    "        <b>\n",
    "    </p>\n",
    "</div>\n",
    "\n",
    "这里 $O_s = I-(\\Sigma^{-1})_s$，直观上来看对于阈值探测器对应的特定的Fock态输出只需要将粒子数分辨探测器对应的多个Fock态概率求和即可。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ed6f8a9",
   "metadata": {},
   "source": [
    "## 代码演示"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a38c31c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-11T02:34:26.526388Z",
     "start_time": "2024-07-11T02:34:26.075730Z"
    }
   },
   "source": [
    "下面简单演示6个模式的高斯玻色采样任务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7994c77a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-11T05:13:21.859427Z",
     "start_time": "2024-07-11T05:13:19.083261Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg baseProfile=\"full\" height=\"5.454545454545454cm\" version=\"1.1\" width=\"22.8cm\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:ev=\"http://www.w3.org/2001/xml-events\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs /><polyline fill=\"none\" points=\"40,30 130,30\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"royalblue\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"10\" x=\"82.5\" y=\"25\" /><text font-size=\"9\" x=\"83\" y=\"20\">S</text><text font-size=\"7\" x=\"95\" y=\"18\">r =1</text><text font-size=\"7\" x=\"95\" y=\"24\">θ =0.0</text><polyline fill=\"none\" points=\"40,60 130,60\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"royalblue\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"10\" x=\"82.5\" y=\"55\" /><text font-size=\"9\" x=\"83\" y=\"50\">S</text><text font-size=\"7\" x=\"95\" y=\"48\">r =1</text><text font-size=\"7\" x=\"95\" y=\"54\">θ =0.0</text><polyline fill=\"none\" points=\"40,90 130,90\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"royalblue\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"10\" x=\"82.5\" y=\"85\" /><text font-size=\"9\" x=\"83\" y=\"80\">S</text><text font-size=\"7\" x=\"95\" y=\"78\">r =1</text><text font-size=\"7\" x=\"95\" y=\"84\">θ =0.0</text><polyline fill=\"none\" points=\"40,120 130,120\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"royalblue\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"10\" x=\"82.5\" y=\"115\" /><text font-size=\"9\" x=\"83\" y=\"110\">S</text><text font-size=\"7\" x=\"95\" y=\"108\">r =1</text><text font-size=\"7\" x=\"95\" y=\"114\">θ =0.0</text><polyline fill=\"none\" points=\"40,150 130,150\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"royalblue\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"10\" x=\"82.5\" y=\"145\" /><text font-size=\"9\" x=\"83\" y=\"140\">S</text><text font-size=\"7\" x=\"95\" y=\"138\">r =1</text><text font-size=\"7\" x=\"95\" y=\"144\">θ =0.0</text><polyline fill=\"none\" points=\"40,180 130,180\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"royalblue\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"10\" x=\"82.5\" y=\"175\" /><text font-size=\"9\" x=\"83\" y=\"170\">S</text><text font-size=\"7\" x=\"95\" y=\"168\">r =1</text><text font-size=\"7\" x=\"95\" y=\"174\">θ =0.0</text><polyline fill=\"none\" points=\"130,30 160,30 190,60 220,60\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"130,60 160,60 190,30 220,30\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"158\" y=\"25\">MZI-PT</text><text font-size=\"7\" x=\"185\" y=\"44\">θ =0.927</text><text font-size=\"7\" x=\"185\" y=\"50\">ϕ =3.142</text><polyline fill=\"none\" points=\"130,90 160,90 190,120 220,120\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"130,120 160,120 190,90 220,90\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"158\" y=\"85\">MZI-PT</text><text font-size=\"7\" x=\"185\" y=\"104\">θ =3.142</text><text font-size=\"7\" x=\"185\" y=\"110\">ϕ =2.198</text><polyline fill=\"none\" points=\"130,150 160,150 190,180 220,180\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"130,180 160,180 190,150 220,150\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"158\" y=\"145\">MZI-PT</text><text font-size=\"7\" x=\"185\" y=\"164\">θ =2.23</text><text font-size=\"7\" x=\"185\" y=\"170\">ϕ =5.378</text><polyline fill=\"none\" points=\"220,60 250,60 280,90 310,90\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"220,90 250,90 280,60 310,60\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"248\" y=\"55\">MZI-PT</text><text font-size=\"7\" x=\"275\" y=\"74\">θ =3.142</text><text font-size=\"7\" x=\"275\" y=\"80\">ϕ =1.778</text><polyline fill=\"none\" points=\"220,120 250,120 280,150 310,150\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"220,150 250,150 280,120 310,120\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"248\" y=\"115\">MZI-PT</text><text font-size=\"7\" x=\"275\" y=\"134\">θ =1.008</text><text font-size=\"7\" x=\"275\" y=\"140\">ϕ =2.71</text><polyline fill=\"none\" points=\"310,30 340,30 370,60 400,60\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"310,60 340,60 370,30 400,30\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"338\" y=\"25\">MZI-PT</text><text font-size=\"7\" x=\"365\" y=\"44\">θ =1.743</text><text font-size=\"7\" x=\"365\" y=\"50\">ϕ =5.066</text><polyline fill=\"none\" points=\"310,90 340,90 370,120 400,120\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"310,120 340,120 370,90 400,90\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"338\" y=\"85\">MZI-PT</text><text font-size=\"7\" x=\"365\" y=\"104\">θ =3.142</text><text font-size=\"7\" x=\"365\" y=\"110\">ϕ =3.833</text><polyline fill=\"none\" points=\"310,150 340,150 370,180 400,180\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"310,180 340,180 370,150 400,150\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"338\" y=\"145\">MZI-PT</text><text font-size=\"7\" x=\"365\" y=\"164\">θ =1.117</text><text font-size=\"7\" x=\"365\" y=\"170\">ϕ =6.085</text><polyline fill=\"none\" points=\"400,60 430,60 460,90 490,90\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"400,90 430,90 460,60 490,60\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"428\" y=\"55\">MZI-PT</text><text font-size=\"7\" x=\"455\" y=\"74\">θ =3.142</text><text font-size=\"7\" x=\"455\" y=\"80\">ϕ =1.499</text><polyline fill=\"none\" points=\"400,120 430,120 460,150 490,150\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"400,150 430,150 460,120 490,120\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"428\" y=\"115\">MZI-PT</text><text font-size=\"7\" x=\"455\" y=\"134\">θ =2.177</text><text font-size=\"7\" x=\"455\" y=\"140\">ϕ =0.201</text><polyline fill=\"none\" points=\"490,30 520,30 550,60 580,60\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"490,60 520,60 550,30 580,30\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"518\" y=\"25\">MZI-PT</text><text font-size=\"7\" x=\"545\" y=\"44\">θ =2.314</text><text font-size=\"7\" x=\"545\" y=\"50\">ϕ =1.34</text><polyline fill=\"none\" points=\"490,90 520,90 550,120 580,120\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"490,120 520,120 550,90 580,90\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"518\" y=\"85\">MZI-PT</text><text font-size=\"7\" x=\"545\" y=\"104\">θ =3.142</text><text font-size=\"7\" x=\"545\" y=\"110\">ϕ =2.4</text><polyline fill=\"none\" points=\"490,150 520,150 550,180 580,180\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"490,180 520,180 550,150 580,150\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"518\" y=\"145\">MZI-PT</text><text font-size=\"7\" x=\"545\" y=\"164\">θ =1.182</text><text font-size=\"7\" x=\"545\" y=\"170\">ϕ =0.023</text><polyline fill=\"none\" points=\"580,60 610,60 640,90 670,90\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"580,90 610,90 640,60 670,60\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"608\" y=\"55\">MZI-PT</text><text font-size=\"7\" x=\"635\" y=\"74\">θ =3.142</text><text font-size=\"7\" x=\"635\" y=\"80\">ϕ =6.006</text><polyline fill=\"none\" points=\"580,120 610,120 640,150 670,150\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"580,150 610,150 640,120 670,120\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"9\" x=\"608\" y=\"115\">MZI-PT</text><text font-size=\"7\" x=\"635\" y=\"134\">θ =0.927</text><text font-size=\"7\" x=\"635\" y=\"140\">ϕ =0.737</text><polyline fill=\"none\" points=\"580,30 670,30\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"teal\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"6\" x=\"622.5\" y=\"25\" /><text font-size=\"9\" x=\"620\" y=\"20\">PS</text><text font-size=\"7\" x=\"635\" y=\"20\">θ =2.189</text><polyline fill=\"none\" points=\"670,60 760,60\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"teal\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"6\" x=\"712.5\" y=\"55\" /><text font-size=\"9\" x=\"710\" y=\"50\">PS</text><text font-size=\"7\" x=\"725\" y=\"50\">θ =2.269</text><polyline fill=\"none\" points=\"670,90 760,90\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"teal\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"6\" x=\"712.5\" y=\"85\" /><text font-size=\"9\" x=\"710\" y=\"80\">PS</text><text font-size=\"7\" x=\"725\" y=\"80\">θ =0.993</text><polyline fill=\"none\" points=\"670,120 760,120\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"teal\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"6\" x=\"712.5\" y=\"115\" /><text font-size=\"9\" x=\"710\" y=\"110\">PS</text><text font-size=\"7\" x=\"725\" y=\"110\">θ =1.654</text><polyline fill=\"none\" points=\"670,150 760,150\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"teal\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"6\" x=\"712.5\" y=\"145\" /><text font-size=\"9\" x=\"710\" y=\"140\">PS</text><text font-size=\"7\" x=\"725\" y=\"140\">θ =3.225</text><polyline fill=\"none\" points=\"580,180 670,180\" stroke=\"black\" stroke-width=\"2\" /><rect fill=\"teal\" height=\"12\" rx=\"0\" ry=\"0\" stroke=\"black\" stroke-width=\"1.5\" width=\"6\" x=\"622.5\" y=\"175\" /><text font-size=\"9\" x=\"620\" y=\"170\">PS</text><text font-size=\"7\" x=\"635\" y=\"170\">θ =2.761</text><polyline fill=\"none\" points=\"220,30 310,30\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"220,180 310,180\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"400,30 490,30\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"400,180 490,180\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"670,30 760,30\" stroke=\"black\" stroke-width=\"2\" /><polyline fill=\"none\" points=\"670,180 760,180\" stroke=\"black\" stroke-width=\"2\" /><text font-size=\"12\" x=\"25\" y=\"30\">0</text><text font-size=\"12\" x=\"25\" y=\"60\">1</text><text font-size=\"12\" x=\"25\" y=\"90\">2</text><text font-size=\"12\" x=\"25\" y=\"120\">3</text><text font-size=\"12\" x=\"25\" y=\"150\">4</text><text font-size=\"12\" x=\"25\" y=\"180\">5</text></svg>"
      ],
      "text/plain": [
       "<svgwrite.drawing.Drawing at 0x2969be96530>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import deepquantum as dq\n",
    "\n",
    "squeezing = [1]*6\n",
    "unitary = np.eye(6)\n",
    "gbs = dq.photonic.GaussianBosonSampling(nmode=6, squeezing=squeezing, unitary=unitary)\n",
    "gbs()\n",
    "gbs.draw() #画出采样线路"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38324390",
   "metadata": {},
   "source": [
    "设置粒子数分辨探测器开始采样并输出Fock态结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e938b606",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-11T05:13:25.148439Z",
     "start_time": "2024-07-11T05:13:23.688117Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "chain 1: 100%|\u001b[32m███████████████████████████████\u001b[0m| 203/203 [00:00<00:00, 446.66it/s]\u001b[0m\n",
      "chain 2: 100%|\u001b[32m███████████████████████████████\u001b[0m| 203/203 [00:00<00:00, 711.65it/s]\u001b[0m\n",
      "chain 3: 100%|\u001b[32m██████████████████████████████\u001b[0m| 203/203 [00:00<00:00, 1191.11it/s]\u001b[0m\n",
      "chain 4: 100%|\u001b[32m██████████████████████████████\u001b[0m| 203/203 [00:00<00:00, 1977.47it/s]\u001b[0m\n",
      "chain 5: 100%|\u001b[32m██████████████████████████████\u001b[0m| 207/207 [00:00<00:00, 2414.95it/s]\u001b[0m"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{|202020>: 2, |022220>: 3, |202200>: 4, |220000>: 24, |020000>: 40, |002000>: 195, |000200>: 83, |002200>: 69, |110202>: 4, |202220>: 18, |222002>: 4, |222020>: 10, |002222>: 15, |020202>: 17, |200202>: 2, |022000>: 4, |020002>: 13, |000000>: 183, |020220>: 3, |000002>: 76, |022200>: 4, |020020>: 3, |220020>: 22, |020200>: 10, |002002>: 27, |000022>: 35, |200200>: 43, |200000>: 40, |000020>: 2, |121002>: 2, |200002>: 9, |202000>: 11, |002020>: 11, |022002>: 7, |122021>: 3, |111120>: 4, |001210>: 1, |221001>: 5, |110200>: 1, |002220>: 11, |200222>: 4}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "gbs.detector = 'pnrd'\n",
    "result = gbs.measure(shots=1024)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a852690",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-11T02:43:22.820297Z",
     "start_time": "2024-07-11T02:43:22.792389Z"
    }
   },
   "source": [
    "设置阈值探测器开始采样并输出Fock态结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7c9d9235",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-07-11T05:13:33.858297Z",
     "start_time": "2024-07-11T05:13:26.187579Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "chain 1: 100%|\u001b[32m██████████████████████████████\u001b[0m| 203/203 [00:00<00:00, 1519.91it/s]\u001b[0m\n",
      "chain 2: 100%|\u001b[32m█████████████████████████████\u001b[0m| 203/203 [00:00<00:00, 20366.54it/s]\u001b[0m\n",
      "chain 3: 100%|\u001b[32m█████████████████████████████\u001b[0m| 203/203 [00:00<00:00, 25460.31it/s]\u001b[0m\n",
      "chain 4: 100%|\u001b[32m█████████████████████████████\u001b[0m| 203/203 [00:00<00:00, 25455.74it/s]\u001b[0m\n",
      "chain 5: 100%|\u001b[32m█████████████████████████████\u001b[0m| 207/207 [00:00<00:00, 23079.32it/s]\u001b[0m"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{|100100>: 41, |101101>: 15, |010010>: 32, |100110>: 16, |100000>: 27, |000110>: 32, |110100>: 16, |101000>: 32, |101001>: 13, |011000>: 13, |001000>: 43, |101100>: 15, |010101>: 10, |011001>: 6, |100010>: 30, |101110>: 8, |110000>: 31, |010001>: 12, |011011>: 7, |001101>: 26, |111001>: 4, |000010>: 32, |001011>: 11, |001010>: 5, |111000>: 11, |100111>: 9, |010111>: 4, |001111>: 11, |010011>: 9, |011101>: 9, |000111>: 23, |001100>: 21, |000101>: 29, |111110>: 3, |010000>: 32, |001001>: 13, |111100>: 6, |100001>: 24, |110010>: 17, |001110>: 13, |000100>: 48, |011010>: 10, |100011>: 13, |101011>: 5, |010110>: 9, |011100>: 12, |110110>: 6, |100101>: 9, |000000>: 26, |000011>: 34, |010100>: 20, |111111>: 4, |000001>: 59, |110101>: 11, |101111>: 3, |011110>: 5, |011111>: 3, |110111>: 1, |110001>: 10, |110011>: 7, |111010>: 9, |101010>: 7, |111011>: 2}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "gbs.detector = 'threshold'\n",
    "result = gbs.measure(shots=1024)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbd14fcb",
   "metadata": {},
   "source": [
    "## 附录"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "450ce075",
   "metadata": {},
   "source": [
    "[1] Lvovsky, Alexander I. \"Squeezed light.\" Photonics: Scientific Foundations, Technology and Applications 1 (2015): 121-163.\n",
    "\n",
    "[2]Bromley, Thomas R., et al. \"Applications of near-term photonic quantum computers: software and algorithms.\" Quantum Science and Technology 5.3 (2020): 034010.\n",
    "\n",
    "[3]Quesada, Nicolás, Juan Miguel Arrazola, and Nathan Killoran. \"Gaussian boson sampling using threshold detectors.\" Physical Review A 98.6 (2018): 062322.\n",
    "\n",
    "[4]J. M. Arrazola and T. R. Bromley, Physical Review Letters 121, 030503 (2018)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dq_v3",
   "language": "python",
   "name": "dq_v3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "168.929px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
